# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def sortedArrayToBST(self, nums: list) -> TreeNode:
        if not nums:
            return None
        mid_index = len(nums) // 2
        mid_root = TreeNode(nums[mid_index])
        left_list = nums[:mid_index]
        right_list = nums[mid_index+1:]
        mid_root.left = self.sortedArrayToBST(left_list)
        mid_root.right = self.sortedArrayToBST(right_list)
        return mid_root